/**
 * 本地存储工具类
 */

// 存储前缀
const STORAGE_PREFIX = 'admin_'

/**
 * 设置localStorage
 * @param key 键名
 * @param value 值
 */
export const setLocalStorage = (key: string, value: any) => {
  localStorage.setItem(`${STORAGE_PREFIX}${key}`, JSON.stringify(value))
}

/**
 * 获取localStorage
 * @param key 键名
 * @param defaultValue 默认值
 */
export const getLocalStorage = <T>(key: string, defaultValue?: T): T | undefined => {
  const value = localStorage.getItem(`${STORAGE_PREFIX}${key}`)
  if (value === null) return defaultValue
  try {
    return JSON.parse(value)
  } catch (error) {
    return defaultValue
  }
}

/**
 * 移除localStorage
 * @param key 键名
 */
export const removeLocalStorage = (key: string) => {
  localStorage.removeItem(`${STORAGE_PREFIX}${key}`)
}

/**
 * 设置sessionStorage
 * @param key 键名
 * @param value 值
 */
export const setSessionStorage = (key: string, value: any) => {
  sessionStorage.setItem(`${STORAGE_PREFIX}${key}`, JSON.stringify(value))
}

/**
 * 获取sessionStorage
 * @param key 键名
 * @param defaultValue 默认值
 */
export const getSessionStorage = <T>(key: string, defaultValue?: T): T | undefined => {
  const value = sessionStorage.getItem(`${STORAGE_PREFIX}${key}`)
  if (value === null) return defaultValue
  try {
    return JSON.parse(value)
  } catch (error) {
    return defaultValue
  }
}

/**
 * 移除sessionStorage
 * @param key 键名
 */
export const removeSessionStorage = (key: string) => {
  sessionStorage.removeItem(`${STORAGE_PREFIX}${key}`)
}

/**
 * 清空所有存储
 */
export const clearStorage = () => {
  localStorage.clear()
  sessionStorage.clear()
}

// 存储键名常量
export const STORAGE_KEYS = {
  TOKEN: 'token',
  USER_INFO: 'user_info',
  LANGUAGE: 'language',
  THEME: 'theme',
  SIDEBAR_STATUS: 'sidebar_status',
  TAGS_VIEW: 'tags_view',
}

// 语言相关
export const setLanguage = (language: string) => {
  setLocalStorage(STORAGE_KEYS.LANGUAGE, language)
}

export const getLanguage = (): string | undefined => {
  return getLocalStorage<string>(STORAGE_KEYS.LANGUAGE)
}

// 主题相关
export const setTheme = (theme: string) => {
  setLocalStorage(STORAGE_KEYS.THEME, theme)
}

export const getTheme = (): string | undefined => {
  return getLocalStorage<string>(STORAGE_KEYS.THEME)
}

// Token相关
export const setToken = (token: string) => {
  setLocalStorage(STORAGE_KEYS.TOKEN, token)
}

export const getToken = (): string | undefined => {
  return getLocalStorage<string>(STORAGE_KEYS.TOKEN)
}

export const removeToken = () => {
  removeLocalStorage(STORAGE_KEYS.TOKEN)
}

// 用户信息相关
export const setUserInfo = (userInfo: any) => {
  setLocalStorage(STORAGE_KEYS.USER_INFO, userInfo)
}

export const getUserInfo = (): any => {
  return getLocalStorage(STORAGE_KEYS.USER_INFO)
}

export const removeUserInfo = () => {
  removeLocalStorage(STORAGE_KEYS.USER_INFO)
}

// 侧边栏状态
export const setSidebarStatus = (status: boolean) => {
  setLocalStorage(STORAGE_KEYS.SIDEBAR_STATUS, status)
}

export const getSidebarStatus = (): boolean => {
  return getLocalStorage<boolean>(STORAGE_KEYS.SIDEBAR_STATUS, false) || false
}

// 标签页
export const setTagsView = (tags: any[]) => {
  setLocalStorage(STORAGE_KEYS.TAGS_VIEW, tags)
}

export const getTagsView = (): any[] => {
  return getLocalStorage<any[]>(STORAGE_KEYS.TAGS_VIEW, []) || []
} 